package com.me.study.app.ext

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition

fun AppCompatActivity.goActivity(cls: Class<*>) {
    goActivity(cls, null)
}

fun AppCompatActivity.goActivity(cls: Class<*>, bundle: Bundle?) {
    val intent = Intent(this, cls)
    bundle?.let {
        intent.putExtras(it)
    }
    startActivity(intent)
}

fun AppCompatActivity.goActivity(cls: Class<*>, bundle: Bundle?, code: Int) {
    val intent = Intent(this, cls)
    bundle?.let {
        intent.putExtras(it)
    }
    startActivityForResult(intent, code)
}

fun AppCompatActivity.goActivity(cls: Class<*>, code: Int) {
    goActivity(cls, null, code)
}

fun Fragment.goActivity(cls: Class<*>) {
    goActivity(cls, null)
}

fun Fragment.goActivity(cls: Class<*>, bundle: Bundle?) {
    val intent = Intent(requireContext(), cls)
    bundle?.let {
        intent.putExtras(it)
    }
    startActivity(intent)
}

fun Fragment.goActivity(cls: Class<*>, bundle: Bundle?, code: Int) {
    val intent = Intent(requireContext(), cls)
    bundle?.let {
        intent.putExtras(it)
    }
    startActivityForResult(intent, code)
}


fun load(context: Context, url: String, success: (Bitmap) -> Unit) {
    Glide.with(context)
        .asBitmap()
        .load(url)
        .into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(
                resource: Bitmap,
                transition: Transition<in Bitmap>?
            ) {
                // 成功返回 Bitmap
                success.invoke(resource)
            }

            override fun onLoadCleared(p0: Drawable?) {
            }

        })
}
